require('./filter');
var Router = require('koa-router');
var service = require('./service');
var router = module.exports = new Router({
  prefix: '/promoter'
});

// 是否注册
router.get('/', loginRequired, function * () {
  var project = this.session.projects[0];
  var data = yield service.acceptProtocol(this, this.session.member.memberId, project.id);
  if (data.agreement) {
    return this.redirect('/promoter/home');
  }
  this.body = this.render('templates/register.html');
});

// 推广员首页
router.get('/home', loginRequired, function * () {
  var self = this;
  var project = this.session.projects[0];
  var data = yield service.promoter(this, this.session.member.memberId, project.id).then(function (data) {
    self.body = self.render('templates/home.html', data);
  }, function (data) {
    self.body = self.render('templates/pause.html', data);
  });
});

// 推广员详情
router.get('/details', loginRequired, function * () {
  var data = yield service.details(this, this.session.member.memberId);
  this.body = this.render('templates/details.html', data, {
    member: this.session.member
  });
});

// 推广的用户
router.get('/members', loginRequired, function * () {
  var data = yield service.members(this, this.session.member.memberId);
  this.body = this.render('templates/members.html', data);
});

// 奖励明细
router.get('/rewards', loginRequired, function * () {
  var data = yield {
    listA: service.rewards(this, this.session.member.memberId, 'BD'),
    listB: service.rewards(this, this.session.member.memberId, 'PC'),
    listC: service.rewards(this, this.session.member.memberId, 'XD'),
  };

  this.body = this.render('templates/rewards.html', data);
});

// 用户行为
router.get('/behavior/:id', loginRequired, function * () {
  var data = yield {
    listA: service.behavior(this, this.session.member.memberId, this.params.id, 'BD'),
    listB: service.behavior(this, this.session.member.memberId, this.params.id, 'PC'),
    listC: service.behavior(this, this.session.member.memberId, this.params.id, 'XD'),
  };
  this.body = this.render('templates/behavior.html', data);
});

// 推广的用户
router.get('/protocol', loginRequired, function * () {
  this.body = this.render('templates/protocol.html');
});

// 千丁推广员积分规则
router.get('/regular', function * (next) {
  this.body = this.render('templates/regular.html');
});

// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //

// 推广的用户
router.get('/ajax/members', loginRequired, function * () {
  var data = yield service.members(this, this.session.member.memberId, this.query.page, this.query.size);
  this.body = this.render(200, data);
});

// 推广明细
router.get('/ajax/rewards', loginRequired, function * () {
  var data = yield service.rewards(this, this.session.member.memberId, this.query.type, this.query.page, this.query.size);
  this.body = this.render(200, data);
});

// 用户行为明细
router.get('/ajax/behavior', loginRequired, function * () {
  var data = yield service.behavior(this, this.session.member.memberId, this.query.invitedId, this.query.type, this.query.page, this.query.size);
  this.body = this.render(200, data);
});